package com.itboye.ihomebank.activity.key;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.ttKey.BleConstant;
import com.itboye.ihomebank.ttKey.Operation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.itboye.ihomebank.util.SPUtils.get;

/**
 * Created by Administrator on 2018/9/6 0006.
 */

public class TTLockTimeTest extends BaseOtherActivity {

    View v_statusbar;
    private ImageView close_icon;// 返回图片
    private TextView add_shap_title_tv;// 标题
//    private EditText lockCurrentTime1;
    private TextView lockCurrentTime;
    private TextView setTtTime;
    public static long dateTimes;

    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

             if(intent.getAction().equals(BleConstant.ACTION_BLE_DISCONNECTED)) {
                try{closeProgressDialog();}catch (Exception e){}

//                finish();
//                Toast.makeText(ActivitySheBei.this, "蓝牙已断开,请重新添加.", Toast.LENGTH_LONG).show();
            }else if (intent.getAction().equals(BleConstant.DEVICETIME)){ //获取锁时间
                 try{closeProgressDialog();}catch (Exception e){}
//                 intent.putExtra("locktime",lockTime);
                 long locktime = intent.getLongExtra("locktime", 0);
                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
                 String format1 = format.format(new Date(locktime));
                 lockCurrentTime.setText(format1);
            }else if (intent.getAction().equals(BleConstant.CHECKLOCKTIME)){ //校验锁时间
                 try{closeProgressDialog();}catch (Exception e){}
                 long oldServiceTime = (long) get(TTLockTimeTest.this, null, SPContants.oldServiceTime, 0l);
                 long oldPhoneTime = (long) get(TTLockTimeTest.this, null, SPContants.oldPhoneTime, 0l);
                 long times = System.currentTimeMillis()-oldPhoneTime+oldServiceTime;
                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
                 String format1 = format.format(new Date(times));
                 lockCurrentTime.setText(format1);
//                 lockCurrentTime1.setText(lockCurrentTime1.getText().toString());
             }
        }
    };


    @Override
    protected int layout_id() {
        return R.layout.activity_ttlocktime;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("锁时间");
        getTTDevcieTime();
        registerReceiver(mReceiver,getIntentFilter());


    }

    private IntentFilter getIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleConstant.ACTION_BLE_DISCONNECTED);
        intentFilter.addAction(BleConstant.DEVICETIME);
        intentFilter.addAction(BleConstant.CHECKLOCKTIME);
        return intentFilter;
    }

    private void getTTDevcieTime() {
        showProgressDialog("正在获取锁时间",true);
        //获取锁的时间
        if (ActivityGuanLiKey.activity.equals("one")){
            KeyFragment.bleSession.setOperation(Operation.DEVICETIME);
            KeyFragment.mTTLockAPI.connect(ActivityGuanLiKey.localKey.getLockMac());
//                    KeyFragment.mTTLockAPI.connect(device);
        }else {//纯门锁
            ActivityGuanLiKey.bleSession.setOperation(Operation.DEVICETIME);
            ActivityGuanLiKey.mTTLockAPI.connect(ActivityGuanLiKey.localKey.getLockMac());
        }
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void finish() {
        super.finish();
        unregisterReceiver(mReceiver);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){

            case R.id.close_icon:
                this.finish();
                break;

            case R.id.setTtTime: //校验时间
                 showProgressDialog("正在校验锁时间",true);
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
//
//                try {
//                    Date   parse = format.parse(lockCurrentTime1.getText().toString());
//                    dateTimes=parse.getTime();
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                    Log.d("====onSetLockTime=",e.getMessage());
//                }

                //校验锁的时间
                if (ActivityGuanLiKey.activity.equals("one")){
                    KeyFragment.bleSession.setOperation(Operation.CHECKDEVICETIME);
                    KeyFragment.mTTLockAPI.connect(ActivityGuanLiKey.localKey.getLockMac());
//                    KeyFragment.mTTLockAPI.connect(device);
                }else {//纯门锁
                    ActivityGuanLiKey.bleSession.setOperation(Operation.CHECKDEVICETIME);
                    ActivityGuanLiKey.mTTLockAPI.connect(ActivityGuanLiKey.localKey.getLockMac());
                }
                break;
        }
    }
}
